package com.HQCourse.vod.vod.service.impl;

import com.HQCourse.vod.vod.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.HQCourse.model.vod.Course;
import com.HQCourse.vod.vod.mapper.CourseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.HQCourse.model.vod.CourseDescription;
import com.example.HQCourse.model.vod.Subject;
import com.example.HQCourse.model.vod.Teacher;
import com.example.HQCourse.vo.vod.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author zww
 * @since 2023-06-25
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private ChapterService chapterService;
    @Autowired
    private VideoService videoService;
    @Autowired
    private CourseDescriptionService courseDescriptionService;
    @Override
    public Map<String, Object> findPage(Page<Course> coursePage, CourseQueryVo courseQueryVo) {
        String title = courseQueryVo.getTitle();
        Long subjectId = courseQueryVo.getSubjectId();
        Long subjectParentId = courseQueryVo.getSubjectParentId();
        Long teacherId = courseQueryVo.getTeacherId();
        QueryWrapper<Course> wrapper=new QueryWrapper<>();
       //不为空封装条件
        if (!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(subjectId)){
            wrapper.eq("subject_id",subjectId);
        }
        if (!StringUtils.isEmpty(subjectParentId)){
            wrapper.eq("subject_parent_id",subjectParentId);
        }
        if (!StringUtils.isEmpty(teacherId)){
            wrapper.eq("teacher_id",teacherId);
        }
        Page<Course> coursePage1 = baseMapper.selectPage(coursePage, wrapper);
        long total = coursePage1.getTotal();
        long pages = coursePage1.getPages();
        List<Course> records = coursePage1.getRecords();
        records.stream().forEach(item-> this.getByNameId(item));
        //封装数据
        Map<String,Object> map=new HashMap<>();
        map.put("totalCount",total);
        map.put("totalpage",pages);
        map.put("records",records);
        return map;
    }

    private Course getByNameId(Course item) {
        Teacher teacher = teacherService.getById(item.getTeacherId());
        if(teacher!=null){
            String name = teacher.getName();
            item.getParam().put("teacherName",name);
        }
        Subject subject = subjectService.getById(item.getSubjectParentId());
        if(subject!=null){
            item.getParam().put("subjectParentTitle", subject.getTitle());
        }
        Subject subjectTwo = subjectService.getById(item.getSubjectId());
        if(subjectTwo!=null){
            item.getParam().put("subjectTitle",subjectTwo.getTitle());
        }
        return item;
    }

    @Override
    public Long saveCourseifo(CourseFormVo courseFormVo) {
        //添加课程基本信息
        Course course=new Course();
        BeanUtils.copyProperties(courseFormVo,course);
        baseMapper.insert(course);
        //添加课程描述信息
        CourseDescription courseDescription=new CourseDescription();
        courseDescription.setDescription(courseFormVo.getDescription());
        //设置id
        courseDescription.setId(course.getId());
        courseDescriptionService.save(courseDescription);
        return course.getId();
    }

    @Override
    public CourseFormVo getCourseInfoById(Long id) {
        Course course = baseMapper.selectById(id);
        //判断获取的course是否为null
        if (course==null){
            return null;
        }
        //查询课程描述信息
        CourseDescription courseDescriptionId = courseDescriptionService.getById(id);

        CourseFormVo courseFormVo=new CourseFormVo();
        BeanUtils.copyProperties(course,courseFormVo);
        //封装描述
        if (courseDescriptionId!=null){
            courseFormVo.setDescription(courseDescriptionId.getDescription());
        }
        return courseFormVo;
    }
    //修改课程的基本信息
    @Override
    public void updateCourseId(CourseFormVo courseFormVo) {
        //修改课程基本信息
        Course course=new Course();
        BeanUtils.copyProperties(courseFormVo,course);
        baseMapper.updateById(course);
        //修改课程描述信息
        CourseDescription courseDescription=new CourseDescription();

        courseDescription.setDescription(courseFormVo.getDescription());
        courseDescription.setId(course.getId());
        courseDescriptionService.updateById(courseDescription);
    }
    //多表查询课程信息
    @Override
    public CoursePublishVo getCoursePublishVo(Long id) {
        CoursePublishVo coursePublishVo=baseMapper.selectCoursePublishVo(id);
         return coursePublishVo;
    }

    @Override
    public Boolean publichVoform(Long id) {
        Course course = new Course();
        course.setId(id);
        course.setPublishTime(new Date());
        course.setStatus(1);
        return this.updateById(course);
    }

    @Override
    public void removeCourseById(Long id) {
        //根据课程id删除小节
        videoService.removeVideoByCourseId(id);
        //根据课程id删除章节
        chapterService.removeChapterByCourseId(id);
        //根据课程id删除描述
         courseDescriptionService.removeById(id);
        //根据课程id删除课程
        baseMapper.deleteById(id);

    }
    //根据课程id查询
    @Override
    public Map<String, Object> getInfoById(Long courseId) {
        Course course = baseMapper.selectById(courseId);
        //浏览数量加一
        course.setViewCount(course.getViewCount()+1);
        baseMapper.updateById(course);
        //根据id查询课程详细信息
        CourseVo courseVo=baseMapper.selectCountVoById(courseId);
        //课程章节小节数据
        List<ChapterVo> treeList = chapterService.getTreeList(courseId);
        CourseDescription courseDescription = courseDescriptionService.getById(courseId);
       //课程所属讲师信息
        Teacher teacher = teacherService.getById(course.getTeacherId());
        Map<String,Object> map=new HashMap();
        map.put("courseVo",courseVo);
        map.put("chapterVoList",treeList);
        map.put("description",courseDescription==null ? "" :courseDescription.getDescription());
        map.put("teacher",teacher);
        map.put("isBuy",false);
        return map;
    }

    @Override
    public Map<String, Object> findPage1(Page<Course> pageParam, CourseQueryVo courseQueryVo) {
        Long subjectId = courseQueryVo.getSubjectId();
        String title = courseQueryVo.getTitle();
        Long teacherId = courseQueryVo.getTeacherId();
        //一级分类
        Long subjectParentId = courseQueryVo.getSubjectParentId();
        QueryWrapper<Course> wrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(subjectId)){
            wrapper.eq("subject_Id",subjectId);
        }
        if(!StringUtils.isEmpty(teacherId)){
            wrapper.eq("teacher_id",teacherId);
        }
        if(!StringUtils.isEmpty(subjectParentId)){
            wrapper.eq("subject_parent_id",subjectParentId);
        }
        Page<Course> coursePage = baseMapper.selectPage(pageParam, wrapper);
        long total = coursePage.getTotal();
        long pages = coursePage.getPages();
        long current = coursePage.getCurrent();
        long size = coursePage.getSize();
        List<Course> records = coursePage.getRecords();
        records.stream().forEach(a->this.getTeacherAnd(a));
             Map<String,Object> map=new HashMap<>();
             map.put("totalCount",total);
             map.put("totalPage",pages);
             map.put("records",records);

        return map;
    }

    private Course getTeacherAnd(Course a) {
        Long teacherId = a.getTeacherId();
        Teacher teacher = teacherService.getById(teacherId);
        if(teacher!=null){
            a.getParam().put("teacherName",teacher.getName());
        }
        //课程分类名称
        Long subjectParentId = a.getSubjectParentId();
        Subject subject = subjectService.getById(subjectParentId);

        if(subject!=null){
            a.getParam().put("subjectParentTitle",subject.getTitle());
        }
        Long subjectId = a.getSubjectId();
        Subject subject1= subjectService.getById(subjectId);
        if(subjectId!=null){
            a.getParam().put("subjectTitle",subject1.getTitle());
        }
        return a;

    }
    //查询所有课程
    @Override
    public List<Course> findlist() {

        List<Course> list = baseMapper.selectList(null);
        list.stream().forEach(item -> {
            this.getTeacherAnd(item);
        });
        return list;
    }


}
